<?php
class IndexAction extends Action{
    public function index(){
	    $Systems = D('Settings');  			//实例化model
	    $result = $Systems->find();
	    $this->assign("index", $result['index']); 
        $this->assign("color", $result['color']); 
        $this->display();
    }
    
public function login(){
		if(empty($_POST['account']) || empty($_POST['password'])){
			$errmsg = '用户名或密码不能为空!';
		}
		if(!empty($errmsg)){
			$this->assign('account',$_POST['account']);
			$this->assign('errmsg',$errmsg);
			$this->display('index');
		}else{
			$Suser = M('Suser');
			$vo=$Suser->join("store on suser.suser_id = store.store_mid")
				->where("suser_id='".$_POST['account']."' and suser_password='".md5($_POST['password'])."'")
				->select();
				
			if ($vo){
				$suser = new Session();
				$sess = array(
					'account'		=>	trim($_POST['account']),
					'qq'			=>	$vo[0]['suser_qq'],
					'pointcard'		=>	$vo[0]['suser_pointcard'],
					'totlepointcard'=>	$vo[0]['suser_totlepointcard'],
					'tel'			=>	$vo[0]['suser_tel'],
					'store_id'		=>	$vo[0]['store_id'],
					'type'			=>	$vo[0]['store_type'],
					'address'		=>	$vo[0]['store_address'],
					'name'			=>	$vo[0]['store_name'],
					'category'		=>	$vo[0]['store_category'],
				);
				$suser->set('suser',$sess);
				$this->redirect('UserAdmin/Index');
			}else{
				$errmsg = '账号或者密码错误！';
				$this->assign('account',$_POST['account']);
				$this->assign('errmsg',$errmsg);
				$this->display('index');
			} 
		}
	}
	
	public function findpsw(){
	 $this->display();
	}
	
	public function findpsword(){
		$suserModel = D('suser');
		$result = $suserModel->where("suser_id='".trim($_REQUEST['id'])."' and suser_idnumber='".trim($_REQUEST['idnumber'])."'")->find();
		$returnData['id']=trim($_REQUEST['id']);
		$returnData['idnumber']=trim($_REQUEST['idnumber']);
		$this->assign('data',$returnData);
		if($result){
			$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
			for($j=0;$j<6;$j++)
			{
			   $pass .= $chars{mt_rand(0,35)};
			}
			$data['suser_password'] = md5($pass);
			if(false!==$suserModel->where("suser_id='".trim($_REQUEST['id'])."' and suser_idnumber='".trim($_REQUEST['idnumber'])."'")->save($data)){
				$maillTitle = "用户密码找回";
				$maillBody ="尊敬的用户：\r\n\r\n";
				$maillBody .="    您的密码已经修改为：".$pass."\r\n";
				$maillBody .="      为了您的账户安全，请及时修改密码！\r\n\r\n";
				$maillBody .="袁衣一中国管理团队 \r\n";
				$sendMailResult = $this->sendMail($result["suser_email"],$maillTitle,$maillBody,'','','',C('MAIL_FROM'),C('MAIL_FROM_NAME'));
				if($sendMailResult){
					$this->display('findpswsuccess');
				}else{
					$this->display('findpsw');
				}
			}
		}else{
			$this->assign("msg","你填写的账户资料不匹配，不能使用取回密码功能，如有疑问请与管理员联系。");
			$this->display('findpsw');
		}
		
	}
	
	/**
	 * 邮件发送。
	 *
	 * @param   $mailTo         收信人邮件(to): array() or string 
	 * @param   $title          邮件标题: string 
	 * @param   $text           邮件内容: string 
	 * @param   $attachment     田间附件: array or string   （默认 = ''）
	 * @param   $mailCc         收件人抄送(cc): array or string    （默认 = ''）
	 * @param   $mailBcc        收件人暗送(bcc): array or string  （默认 = ''）
	 * @param   $mailFrom       发信人邮件: string  （默认 = config.php/MAIL_FROM）
	 * @param   $mailFromName   发信人名: string    （默认 = config.php/MAIL_FROM_NAME）
	 *
	 * @return  成功: true
	 *          失败: false
	 */
	public function sendMail($mailTo, $title, $text, $attachment='', $mailCc='', $mailBcc='', $mailFrom=MAIL_FROM, $mailFromName=MAIL_FROM_NAME)
	{
		vendor("PHPMailer.class#phpmailer");
		$result = true;
		$mail = new PHPMailer();
		$mail->IsSMTP();
		$mail->CharSet="UTF-8";
		$mail->Encoding="base64";
		$mail->SetLanguage('zh_cn','/mailutil/language/');
		$mail->WordWrap = 120;
		$mail->IsHTML(false);

		$mail->Host = C('MAIL_SMTP_HOST'); //SMTP服务
		$mail->SMTPAuth = C('MAIL_SMTP_AUTH'); //SMTP验证标记
		$mail->Username = C('MAIL_USER'); //邮件用户名
		$mail->Password = C('MAIL_PASSWORD'); //邮件用户密码
		$mail->From = $mailFrom;//邮件发送地址

		if(is_array($mailTo) && count($mailTo)>0)
		{
			foreach($mailTo as $one)
			{
				$one = trim($one);
				$mail->AddAddress($one);
			}
		}
		else if(is_string($mailTo) && strlen($mailTo)>0)
		{
			$mailToList = explode(';', $mailTo);
			foreach($mailToList as $one)
			{
				$one = trim($one);
				if($one != "")
				{
					$mail->AddAddress($one);
				}
			}
		}
		else
		{
			$result = false;
		}

		if($result === true)
		{
			//邮件标题
			$mail->Subject = $title;
			//邮件内容
			$mail->Body = $text;
				
			//附件
			if(is_array($attachment) && count($attachment)>0)
			{
				foreach($attachment as $one)
				{
					$one = trim($one);
					$name = basename($one);
					$one = mb_convert_encoding($one, "Windows-31j", ENCODE);
					$mail->AddAttachment($one, $name);
				}
			}
			else if(is_string($attachment) && strlen(trim($attachment))>0)
			{
				$one = trim($attachment);
				$name = basename($one);
				$one = mb_convert_encoding($one, "Windows-31j", ENCODE);
				$mail->AddAttachment($one, $name);
			}
				
			//CC
			if(is_array($mailCc) && count($mailCc)>0)
			{
				foreach($mailCc as $one)
				{
					$one = trim($one);
					$mail->AddCC($one);
				}
			}
			else if(is_string($mailCc) && strlen($mailCc)>0)
			{
				$mailCcList = explode(';', $mailCc);
				foreach($mailCcList as $one)
				{
					$one = trim($one);
					if($one != "")
					{
						$mail->AddCC($one);
					}
				}
			}
				
			//BCC
			if(is_array($mailBcc) && count($mailBcc)>0)
			{
				foreach($mailBcc as $one)
				{
					$one = trim($one);
					$mail->AddBCC($one);
				}
			}
			else if(is_string($mailBcc) && strlen($mailBcc)>0)
			{
				$mailBccList = explode(';', $mailBcc);
				foreach($mailBccList as $one)
				{
					$one = trim($one);
					if($one != "")
					{
						$mail->AddBCC($one);
					}
				}
			}
				
			//发信人名
			$mail->FromName = $mailFromName;
				
			//发信
			if (!$mail->Send())
			{
				$errorNo=$mail->ErrorCode;
				$this->assign("msg","Mailer Error: ".$mail->ErrorInfo." error no ===> ".$errorNo);
				//$log->error("Mailer Error: ".$mail->ErrorInfo." error no ===> ".$errorNo);
				//ECHO "msg","Mailer Error: ".$mail->ErrorInfo." error no ===> ".$errorNo.$mail->Host;
				//dump($mail);
				$result = false;
			}
			else
			{
				$result = true;
			}
		}
		
		return $result;

	}
}
?>